#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : lei.wang
import yaml
import os
from dotenv import load_dotenv
from pathlib import Path
from starlette.config import Config
from app.core.pkg.log import logger

load_dotenv()

env_mode = os.getenv("SERVER_ENV", "development")
logger.info("当前配置环境为: {}".format(env_mode))
current_file_dir = Path(__file__).parent
env_path = current_file_dir.parent.parent / "config-{}.yaml".format(env_mode)


# 定义一个函数来读取 YAML 文件并返回字典
def read_yaml_config(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            return yaml.safe_load(file)
    except FileNotFoundError:
        print(f"Error: The YAML file {file_path} was not found.")
        return {}
    except yaml.YAMLError as e:
        print(f"Error parsing YAML file: {e}")
        return {}


config = Config(environ=read_yaml_config(env_path))
